package cn.scau.edu.software.lgh.controller;

import cn.scau.edu.software.lgh.entity.NetWork;
import cn.scau.edu.software.lgh.entity.Route;
import cn.scau.edu.software.lgh.service.NetworkSecviceImpl;
import cn.scau.edu.software.lgh.service.RouteServiceImpl;

import java.util.Vector;

public class RIPController {

    private Vector<NetWork> netWorks = new Vector<>();

    private Vector<Route> routes = new Vector<>();

    public String addNetwork(NetWork netWork){
        for(NetWork net:netWorks){
            if(net.getNetworkId().equals(netWork.getNetworkId())){
                return netWork.getNetworkId()+"此网络已存在";
            }
        }
        if(netWorks.add(netWork)){
            return "生成网络"+netWork.getNetworkId()+"成功";
        }else{
            return "生成网络"+netWork.getNetworkId()+"失败";
        }
    }

    public String addRoute(Route route){
        for(Route router:routes){
            if(router.getRouteId().equals(route.getRouteId())){
                return route.getRouteId()+"此路由器已存在";
            }
        }
        if(routes.add(route)){
            return "生成路由器"+route.getRouteId()+"成功";
        }else{
            return "生成路由器"+route.getRouteId()+"失败";
        }
    }


    public String linkNetworkToRoute(String routeId,String netWorkId){
        Route router=null;
        NetWork net=null;
        for(Route route:routes){
              if(route.getRouteId().equals(routeId)){
                  router=route;
              }
        }
        for(NetWork netWork:netWorks){
            if(netWork.getNetworkId().equals(netWorkId)){
                net=netWork;
            }
        }
        if(router==null){
            return "路由器"+routeId+"不存在";
        }
        if(net==null){
            return "网络"+netWorkId+"不存在";
        }
        if(router!=null&&net!=null){
            RouteServiceImpl.addNetWorkToRoute(router,net);
            NetworkSecviceImpl.addRouteToNetwork(net,router);
            return "路由器"+routeId+"网络"+netWorkId+"连接成功";
        }else{
            return "路由器"+routeId+"网络"+netWorkId+"连接失败";
        }
    }

    public void createWholeNetwork(){
        System.out.println(addNetwork(NetworkSecviceImpl.createNetwork("1")));
        System.out.println(addNetwork(NetworkSecviceImpl.createNetwork("2")));
        System.out.println(addNetwork(NetworkSecviceImpl.createNetwork("3")));
//        System.out.println(addNetwork(NetworkSecviceImpl.createNetwork("4")));
//        System.out.println(addNetwork(NetworkSecviceImpl.createNetwork("5")));
        System.out.println(addRoute(RouteServiceImpl.createRoute("A")));
        System.out.println(addRoute(RouteServiceImpl.createRoute("B")));
        System.out.println(addRoute(RouteServiceImpl.createRoute("C")));
        System.out.println(linkNetworkToRoute("A","1"));
        System.out.println(linkNetworkToRoute("A","2"));
        System.out.println(linkNetworkToRoute("B","2"));
        System.out.println(linkNetworkToRoute("B","3"));
        System.out.println(linkNetworkToRoute("C","3"));
        System.out.println(linkNetworkToRoute("C","1"));
//        System.out.println(linkNetworkToRoute("C","5"));
    }

    public static void main(String[] args){
        RIPController ripController= new RIPController();
        ripController.createWholeNetwork();
    }
}
